package com.nidu.demo.dict.executor.command;

import com.alibaba.cola.dto.SingleResponse;
import com.nidu.demo.dict.ability.DictTypeDomainService;
import com.nidu.demo.dict.dto.DictDataUpdateCmd;
import com.nidu.demo.dict.ability.DictDataDomainService;
import com.nidu.demo.dict.convertor.DictDataConvertor;
import com.nidu.demo.dict.dto.DictDataCO;
import com.nidu.demo.dict.gateway.DictDataGateway;
import com.nidu.demo.dict.model.DictData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 字典类型更新命令执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class DictDataUpdateCmdExe {

    private final DictDataGateway gateway;

    private final DictDataDomainService domainService;

    private final DictDataConvertor convertor;

    private final DictTypeDomainService dictTypeDomainService;

    public SingleResponse<DictDataCO> execute(DictDataUpdateCmd cmd) {
        // 1. 转换为领域对象
        DictData dictData = DictData.update(cmd.getId(), cmd.getLabel(), cmd.getValue(), cmd.getDictType(), cmd.getColorType(),
                cmd.getCssClass(), cmd.getRemark(), cmd.getSortOrder());

        // 2. 检查公司是否存在
        dictTypeDomainService.validateDictType(dictData.getDictType());
        // 3. 检查value是否已存在
        domainService.validateDuplicate(dictData.getDictType(), dictData.getValue(), dictData.getId());

        // 4. 调用网关保存
        DictData save = gateway.update(dictData);

        // 5. 转换为DTO返回
        DictDataCO dictDataCO = convertor.toClientObject(save);

        return SingleResponse.of(dictDataCO);
    }
} 